package org.blogsite.greencode.struts2.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.blogsite.greencode.bean.Schooling_Instructor_Course;
import org.blogsite.greencode.bean.TeachingSupport_Course;
import org.blogsite.greencode.service.SchoolingManageService;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
 * @author：wzj E-mail: jsun789@gmail.com
 * @date：2010-7-25 下午07:10:58
 */
public class SchoolingManageAction extends ActionSupport {

	private SchoolingManageService schoolingManageService;
	private Schooling_Instructor_Course schoolingInstructorCourse;
	private String searchText;
	private String searchRadio;
	HttpServletRequest hs;
	private SimpleDateFormat dateformat;
	

	// private Object jsonModel;

	/*
	 * 
	 * public String ajaxShowCourseDetail() throws Exception {
	 * System.out.println("hello..."); // TeachingSupport_Course course=
	 * schoolingManageService.getCourseById(4); // setJsonModel(course); return
	 * "SUCCESS"; }
	 */
	// 显示授课保存单
	public String displaySave() throws Exception {
		hs = ServletActionContext.getRequest();
		// System.out.println("hello"+schoolingManageService.getAllCourseName().get(0));
		// 调用服务层,获取所有讲师的集合,显示在表单select,供用户选择
		hs.setAttribute("InstructorNames", schoolingManageService
				.getAllInstructorName());
		// 调用服务层,获取所有课程的集合,显示在表单select,供用户选择
		hs.setAttribute("CourseNames", schoolingManageService
				.getAllCourseName());

		hs.setAttribute("add", "1");
		return "SUCCESS_DISPLAY";
	}

	// 显示授课单修改
	public String displayModify() throws Exception {

//		sid,courseName,instructorName,place
		hs = ServletActionContext.getRequest();
		// System.out.println("hello"+schoolingManageService.getAllCourseName().get(0));
		
		// 调用服务层,获取所有讲师的集合
		hs.setAttribute("InstructorNames", schoolingManageService
				.getAllInstructorName());
		// 调用服务层,获取所有课程的集合
		hs.setAttribute("CourseNames", schoolingManageService
				.getAllCourseName());
		hs.setAttribute("sid", hs.getParameter("sid"));
		hs.setAttribute("courseId", hs.getParameter("courseId"));
		hs.setAttribute("instructorId", hs.getParameter("instructorId"));
		hs.setAttribute("courseName", hs.getParameter("courseName"));
		hs.setAttribute("instructorName", hs.getParameter("instructorName"));
		hs.setAttribute("place", hs.getParameter("place"));
		hs.setAttribute("date", hs.getParameter("date"));
		System.out.println(hs.getParameter("place")+"da");
		hs.setAttribute("modify", "1");
		return "SUCCESS_DISPLAY";
	}  
	/*显示删除授课单*/
	public String displayDelete() throws Exception {
		hs = ServletActionContext.getRequest();
		hs.setAttribute("search", "1");
		hs.setAttribute("delete","1");
		return "SUCCESS_DISPLAY";
	}
	/*修改之前,先查询*/
	public String beforeModify() throws Exception {
		System.out.println("before modify");
		hs = ServletActionContext.getRequest();
		hs.setAttribute("search", "1");
		hs.setAttribute("beforemodify","1");
		return "SUCCESS_DISPLAY";
	}
	

	// 显示授课单查询
	public String displaySearch() throws Exception {

		hs = ServletActionContext.getRequest();
		hs.setAttribute("search", "1");
		hs.setAttribute("searchtitle", "1");
		return "SUCCESS_DISPLAY";
	}

	// 保存授课单
	public String save() throws Exception {
//		System.out.println("hello");
//		schoolingInstructorCourse.setDate(getDateformat().format(new Date()));
		schoolingManageService.save(schoolingInstructorCourse);
		hs = ServletActionContext.getRequest();
		hs.setAttribute("savesuccess", "1");
		
		return "SUCCESS_SAVE";
	}
	//修改授课单
	public String modify() throws Exception{
		schoolingManageService.updateTimeTable(schoolingInstructorCourse);
		hs = ServletActionContext.getRequest();
		hs.setAttribute("modifysuccess", "1");
		return "SUCCESS_MODIFY";
	}
	//删除授课单
	public String delete() throws Exception{
		
		hs = ServletActionContext.getRequest();
		try {
			
			System.out.println("eeee:"+hs.getParameter("sid"));
//			schoolingInstructorCourse.setSid(4);
//			schoolingInstructorCourse.setSid(Integer.parseInt(hs.getParameter("sid")));
			schoolingManageService.deleteTimeTable(new Schooling_Instructor_Course(Integer.parseInt(hs.getParameter("sid"))));
			hs = ServletActionContext.getRequest();
			hs.setAttribute("deletesuccess", "1");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "SUCCESS_DELETE";
	}
	

	// 查询授课单
	public String search() throws Exception {
		List list;
		HttpServletRequest hs = ServletActionContext.getRequest();
		if (getSearchRadio().equals("all")||"".equals(getSearchRadio())||getSearchRadio()==null) {
			// 调用服务层,获取所有授课表的集合
			list = schoolingManageService.getAllTimeTable();
			hs.setAttribute("allTimeTable", list);
			if (list.iterator().hasNext() == false) {
				hs.setAttribute("searchnodata", "1");
			} else
				hs.setAttribute("searchhavedata", "1");

		} else if (getSearchRadio().equals("cno")) {
			// 调用服务层,获取指定课程号所有授课表的集合
			try {

				list = schoolingManageService.getTimeTableByCno(Integer
						.parseInt(getSearchText()));
				if (list.iterator().hasNext() == false) {
					hs.setAttribute("searchnodata", "1");
				} else
					hs.setAttribute("searchhavedata", "1");
				hs.setAttribute("allTimeTable", list);
			} catch (Exception e) {
				//格式转换异常,课程号也一定是错的,没数据的,直接返回结果
				hs.setAttribute("searchnodata", "1");
			}
		} else if (getSearchRadio().equals("ino")) {
			// 调用服务层,获取指定讲师号所有授课表的集合
			try {
				list = schoolingManageService.getTimeTableByIno(Integer
						.parseInt(getSearchText()));
				hs.setAttribute("allTimeTable", list);
				if (list.iterator().hasNext() == false) {
					hs.setAttribute("searchnodata", "1");
				} else
					hs.setAttribute("searchhavedata", "1");
				
			} catch (Exception e) {
				//格式转换异常,讲师号也一定是错的,没数据的,直接返回结果
				hs.setAttribute("searchnodata", "1");
			}
		} else if (getSearchRadio().equals("blear")) {
			// 调用服务层,获取模糊查询所有授课表的集合
			list = schoolingManageService.getTimeTableByBlear(getSearchText());
			hs.setAttribute("allTimeTable", list);
			if (list.iterator().hasNext() == false) {
				hs.setAttribute("searchnodata", "1");
			} else
				hs.setAttribute("searchhavedata", "1");
		}
		hs.setAttribute("search", "1");
		return "SUCCESS_DISPLAY";
	}

	public SchoolingManageService getSchoolingManageService() {
		return schoolingManageService;
	}

	public void setSchoolingManageService(
			SchoolingManageService schoolingManageService) {
		this.schoolingManageService = schoolingManageService;
	}

	public Schooling_Instructor_Course getSchoolingInstructorCourse() {
		return schoolingInstructorCourse;
	}

	public void setSchoolingInstructorCourse(
			Schooling_Instructor_Course schoolingInstructorCourse) {
		this.schoolingInstructorCourse = schoolingInstructorCourse;
	}

	public String getSearchText() {
		return searchText;
	}

	public void setSearchText(String searchText) {
		this.searchText = searchText;
	}

	public String getSearchRadio() {
		return searchRadio;
	}

	public void setSearchRadio(String searchRadio) {
		this.searchRadio = searchRadio;
	}

	public SimpleDateFormat getDateformat() {
		return dateformat;
	}

	public void setDateformat(SimpleDateFormat dateformat) {
		this.dateformat = dateformat;
	}
	

	
	

	// public Object getJsonModel() {
	// return jsonModel;
	// }
	// public void setJsonModel(Object jsonModel) {
	// this.jsonModel = jsonModel;
	// }

}
